# Makefile for building and managing the project

# Variables
SHELL := /bin/bash
GO := go
DISTPATH := $(shell pwd)/dist
VERSION := $(shell git describe --tags)
BUILD := $(shell date +%FT%T%z)
GIT_COMMIT := $(shell git rev-list -1 HEAD)

# LDFLAGS for the Go build
LDFLAGS := -s -w \
	-X 'github.com/xuelang-group/suanpan-appconnector/cmd/appconnector/global.Version=$(VERSION)' \
	-X 'github.com/xuelang-group/suanpan-appconnector/cmd/appconnector/global.Build=$(GIT_COMMIT)--$(BUILD)' \
	-X 'github.com/xuelang-group/suanpan-appconnector/cmd/appconnector/global.Type=tiny'

# Targets
.PHONY: all build clean test

all: build

build: check-distpath
	@echo "Version: $(VERSION)"
	@echo "Build: $(GIT_COMMIT)--$(BUILD)"
	CGO_ENABLED=0 $(GO) build -v -ldflags="$(LDFLAGS)" -o $(DISTPATH)/connectgo cmd/appconnector/main.go

check-distpath:
	@if [[ "$(DISTPATH)" != /* ]]; then \
		echo "DISTPATH must be an absolute path. Current value: $(DISTPATH)"; \
		exit 1; \
	fi
	@if [ ! -d "$(DISTPATH)" ]; then \
		echo "Creating directory $(DISTPATH)"; \
		mkdir -p $(DISTPATH); \
	fi

clean:
	rm -f $(DISTPATH)/connectgo

test:
	$(GO) test ./...

# Additional targets from build.sh
build-test:
	$(GO) build -o test main.go

build-no-debug:
	$(GO) build -ldflags="-s -w" -o test_no_debug_and_symbol main.go
